package com.tenement.action;

import com.tenement.domain.TenementSector;
import com.tenement.domain.TenementUser;
import com.tenement.service.SectorService;
import com.tenement.utils.Message;
import com.tenement.utils.Tools;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : sectorAction  //类名
 * @Description : 通知用户关联表  //描述
 * @Author : 易嵘  //作者
 * @Date: 2020-06-07 10:43  //时间
 */
@Controller
@RequestMapping("sectorAction")
public class SectorAction {

    @Resource
    SectorService sectorService;

    @ResponseBody
    @RequestMapping("selectAll")
    public List<TenementSector> selectAll() {
        return sectorService.selectAll();
    }

    @ResponseBody
    @RequestMapping("findAll")
    public Map<String, Object> findAll(TenementSector sector,
                                       @RequestParam(value = "startDate", required = false) String startDate,
                                       @RequestParam(value = "endDate", required = false) String endDate,
                                       @RequestParam("page") Integer page,
                                       @RequestParam("limit") Integer limit) {
        List<TenementSector> list = sectorService.findAll(sector, page, limit, startDate, endDate);
        Long overCount = sectorService.overCount(sector, startDate, endDate);

        return Tools.maps(overCount, list);
    }

    @ResponseBody
    @RequestMapping("addOrUpdate")
    public Message addOrUpdate(TenementSector sector, HttpSession session) {
        int row;
        TenementUser user = (TenementUser) session.getAttribute("user");
        if (StringUtils.isEmpty(sector.getSectorId())) {
            row = sectorService.add(sector, user);
        } else {
            row = sectorService.update(sector, user);
        }
        return Tools.rows(row);
    }

    @ResponseBody
    @RequestMapping("deleteById")
    public Message deleteById(String id) {
        int row = sectorService.deleteById(id);
        return Tools.rows(row);
    }

    @ResponseBody
    @RequestMapping("findById")
    public TenementSector findById(String id) {
        return sectorService.findById(id);
    }

    @ResponseBody
    @RequestMapping("deleteAll")
    public Message deleteAll(@RequestParam(value = "ids[]", required = false) String ids[]) {
        int row = sectorService.deleteAll(ids);
        return Tools.rows(row);
    }

    /**
     * 日期格式化
     *
     * @param binder
     */
    /*@InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); // true:允许输入空值，false:不能为空值

    }*/
}
